package com.example.demo.service;

import com.example.demo.dao.InsertDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

@Service
public class InsertServiceImpl implements InsertService{
    @Autowired
    private InsertDao id;

    public boolean simpleInsert(Map<String,String[]> params){
        Class c = id.getClass();
        Map<String,Object> sqlParams = new HashMap<>();
        //提取data
        String interfaceName="";
        for(String key:params.keySet()){
            if(key.equalsIgnoreCase("flag")){
                interfaceName=params.get("flag")[0];
            }
            else{
                sqlParams.put(key,params.get(key)[0]);
            }
        }
        Method[] methods = c.getMethods();
        for (int i = 0; i<methods.length;i++){
            if(interfaceName.equals(methods[i].getName())){
                try {
                    methods[i].invoke(id,sqlParams);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                    return false;
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                    return false;
                }
            }
        }

        return true;
    }
}
